package com.agent.vpsserver.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2019/5/20,10:03
 * @since 1.0.0     2019/5/20,10:03
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile(value = {"dev", "test", "pro"})
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

    @Bean
    public Docket controllerApi() {
        //可以添加多个header或参数
        ParameterBuilder apiParameterBuilder = new ParameterBuilder();
        apiParameterBuilder
                //参数类型支持header, cookie, body, query etc
                .parameterType("header")
                .name(HttpHeaders.AUTHORIZATION)
                .defaultValue("Bearer {\"token\":\"\",\"sign\":\"\",\"timeStamp\":1234567891,\"sysType\":1,\"clientVersion\":\"1.0.0\",\"deviceBrand\":\"荣耀20\",\"deviceType\":\"HUAWEI\",\"noSign\":\"true\"}")
                .description("api接口固定参数字段")
                .modelRef(new ModelRef("string"))
                .required(false).build();
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(apiParameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("API文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.agent.vpsserver"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("放心选接口文档")
                .description("描述：放心选-服务API文档")
                .termsOfServiceUrl("http://www.baidu.com/")
                .contact(new Contact("", "http://www.baidu.com/", "****@****.com"))
                .version("版本号:1.0.0")
                .build();
    }
}